import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * tcp服务器线程处理类
 */
public class ServerThread extends Thread {
   // 和本线程相关的Socket
   static protected int setLen = 32;
   Socket socket;
   public ServerThread(Socket socket) {
      this.socket=socket;
   }

   // 线程执行的操作，响应客户端的请求
   @Override
   public void run() {
      InputStream is=null;
      InputStreamReader isr=null;
      BufferedReader br=null;
      OutputStream os=null;
      PrintWriter pw =null;

      try {
         // 获取输入流，并读取客户端发送的信息
         /*字节输入流*/
         System.out.printf("开始接收用户信息:");
         is=socket.getInputStream();
         isr=new InputStreamReader(is);// 将字节流转换为字符流
         br=new BufferedReader(isr);// 为输入流添加缓冲
         String info=null;// 循环读取客户端信息
         String field0="0";
         String field1="0";
         String field2="0";
         String field3="0";
         String field4="0";
         String field5="0";
         String file="0";
         while ((info=br.readLine()) != null) {
            //System.out.printf("客户端传送了用户名及密码：%s%n", info);
            field0 = info.substring(0,1).trim();
            field1 = info.substring(1,1+setLen).trim();
            field2 = info.substring(1+setLen,1+2*setLen).trim();
            field3 = info.substring(1+2*setLen,1+3*setLen).trim();
            field4 = info.substring(1+3*setLen,1+4*setLen).trim();
            field5 = info.substring(1+4*setLen,1+5*setLen).trim();
            System.out.printf("field0:"+field0+"%n");
            System.out.printf("field1:"+field1+"%n");
            System.out.printf("field2:"+field2+"%n");
            System.out.printf("field3:"+field3+"%n");
            System.out.printf("field2:"+field4+"%n");
            System.out.printf("field3:"+field5+"%n");
            break;
         }
         System.out.printf("服务端接收了客户端的信息，正在处理请稍后:");
         String Output=" ";
         //功能1:客户端登录
         if(field0.compareTo("1") == 0){
            System.out.printf("客户端"+field1+":请求登录%n");
            System.out.printf("客户端"+field1+"发送的密码为"+field2+"%n");
            /*
               与数据库相连，用于检测账号密码的函数。
               Output=login(,);
            */
            if(field1.compareTo("xym") == 0 && field2.compareTo("yzk") == 0)
            {
               Output = "0";
            }
            else
            {
               Output = "1";
            }
         }
         //功能2:客户端传送文字文件
         if(field0.compareTo("2") == 0){
            System.out.printf("用户请求提交答案%n");
            BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(new File("C:/Server/"+field1+"-"+field2+"-"+field3+"-"+field4+"answer.txt")));
            BufferedInputStream bis=new BufferedInputStream(is);
            int input=-1;
            while((input=bis.read())!=-1){
               bos.write(input);
            }
            bos.flush();
            bos.close();
            Output="0";
            System.out.printf("提交答案完成%n");
         }
         //功能3:客户端传送音频文件
         if(field0.compareTo("3") == 0){
            System.out.printf("用户请求上传题解%n");
            BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(new File("C:/Server/"+field1+"-"+field2+"-"+field3+"-"+field4+"-"+field5+"solution.txt")));
            BufferedInputStream bis=new BufferedInputStream(is);
            int input=-1;
            while((input=bis.read())!=-1){
               bos.write(input);
            }
            bos.flush();
            bos.close();
            Output="0";
            System.out.printf("上传题解完成%n");
         }
         if(field0.compareTo("4") == 0){
            System.out.printf("用户请求上传题目%n");
            BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(new File("C:/Server/"+field1+"-"+field2+"-"+field3+"-"+field4+"problem.txt")));
            BufferedInputStream bis=new BufferedInputStream(is);
            int input=-1;
            while((input=bis.read())!=-1){
               bos.write(input);
            }
            bos.flush();
            bos.close();
            Output="0";
            System.out.printf("上传题目完成%n");
         }

         // 获取输出流，响应客户端的请求
         os=socket.getOutputStream();
         pw=new PrintWriter(os); // 包装为打印流
         pw.write(Output+"\n");
         pw.flush();

         //功能5:客户端下载文字文件
         if(field0.compareTo("5") == 0){
            System.out.printf("客户端请求下载文字文件%n");
            BufferedInputStream bis=new BufferedInputStream(new FileInputStream("C:/Server/"+field1+"-"+field2+"-"+field3+"-"+field4+"problem.txt"));
            BufferedOutputStream bos = new BufferedOutputStream(os);
            int input;
            while ((input = bis.read()) != -1) {
               bos.write(input);
            }
            System.out.printf("传输完成!%n");
            bos.flush();
            bis.close();
            Output="0";
         }
         socket.shutdownOutput();
      } catch (IOException e) {
         e.printStackTrace();
      }finally {
         try {
            // 关闭资源
            if (pw != null) {
               pw.close();
            }
            if (os != null) {
               os.close();
            }
            if (br != null) {
               br.close();
            }
            if (isr != null) {
               isr.close();
            }
            if (is != null) {
               is.close();
            }
            if (socket != null) {
               socket.close();
            }
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
      Server.nowCount--;
   }
}
